<% layout('/layouts/_default.html', {title: '用户选择', libs: ['dataGrid']}){ %>
<div class="main-content">
  <div class="box box-main">
    <div class="box-body">
      <#form:form id="searchForm" action="${ctxPath}/admin/listData" method="post" class="form-inline "
          data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">
        <#form:hidden name="status" value="${isNotBlank(user.status) ? user.status : '0'}"/>
        <#form:hidden name="userType" value="${user.userType}"/>
        <div class="form-group">
          <label class="control-label">账号：</label>
          <div class="control-inline">
            <#form:input name="loginCode" maxlength="100" class="form-control width-90"/>
          </div>
        </div>
        <div class="form-group">
          <label class="control-label">昵称：</label>
          <div class="control-inline">
            <#form:input name="userName" maxlength="100" class="form-control width-90"/>
          </div>
        </div>
        <div class="form-group">
          <label class="control-label">邮箱：</label>
          <div class="control-inline">
            <#form:input name="email" maxlength="300" class="form-control width-90"/>
          </div>
        </div>
        <div class="form-group">
          <label class="control-label">手机：</label>
          <div class="control-inline">
            <#form:input name="mobile" maxlength="100" class="form-control width-90"/>
          </div>
        </div>
        <div class="form-group">
          <label class="control-label">电话：</label>
          <div class="control-inline">
            <#form:input name="phone" maxlength="100" class="form-control width-90"/>
          </div>
        </div>
        <div class="form-group">
          <button type="submit" class="btn btn-primary btn-sm">查询</button>
          <button type="reset" class="btn btn-default btn-sm">重置</button>
        </div>
      </#form:form> 
      <div class="row">
        <div class="col-xs-10 pr10">
          <table id="dataGrid"></table>
          <div id="dataGridPage"></div>
        </div>
        <div class="col-xs-2 pl0">
          <div id="selectData" class="tags-input"></div>
        </div>
      </div>
    </div>
  </div>
</div>
<% } %>
<script>
var selectData = ${isNotBlank(selectData!) ? selectData! : "{\}"},
selectNum = 0, dataGrid = $('#dataGrid').dataGrid({
  searchForm: $("#searchForm"),
  columnModel: [
    {header:'登录账号', name:'loginCode', index:'a.login_code', width:200, align:"center"},
    {header:'用户昵称', name:'userName', index:'a.user_name', width:200, align:"center"},
    {header:'电子邮箱', name:'email', index:'a.email', width:200, align:"center"},
    {header:'手机号码', name:'mobile', index:'a.mobile', width:200, align:"center"},
    {header:'办公电话', name:'phone', index:'a.phone', width:200, align:"center"},
    {header:'更新时间', name:'updateDate', index:'a.update_date', width:200, align:"center"},
    {header:'状态', name:'status', index:'a.status', width:100, align:"center", formatter: function(val, obj, row, act){
      return js.getDictLabel([{"dictValue":"0","cssStyle":"","cssClass":"","dictLabel":"正常"},{"dictValue":"1","cssStyle":"color:#f00;","cssClass":"","dictLabel":"删除"},{"dictValue":"2","cssStyle":"color:#f00;","cssClass":"","dictLabel":"停用"},{"dictValue":"3","cssStyle":"color:#fa0;","cssClass":"","dictLabel":"冻结"},{"dictValue":"4","cssStyle":"","cssClass":"","dictLabel":"待审"},{"dictValue":"5","cssStyle":"","cssClass":"","dictLabel":"驳回"},{"dictValue":"9","cssStyle":"color:#aaa;","cssClass":"","dictLabel":"草稿"}], val, '未知', true);
    }},
    {header:'行数据', name:'rowData', hidden:true, formatter: function(val, obj, row, act){
      return JSON.stringify(row);
    }}
  ],
  autoGridHeight: function(){
    var height = $(window).height() - $('#searchForm').height() - $('#dataGridPage').height() - 73;
    $('.tags-input').height($('.ui-jqgrid').height() - 10);
    return height;
  },
  showCheckbox: '${checkbox!}' == 'true',
  multiboxonly: false, // 单击复选框时再多选
  ajaxSuccess: function(data){
    $.each(selectData, function(key, value){
      dataGrid.dataGrid('setSelectRow', key);
    });
    initSelectTag();
  },
  onSelectRow: function(id, isSelect, event){
    if ('${checkbox!}' == 'true'){
      if(isSelect){
        selectData[id] = JSON.parse(dataGrid.dataGrid('getRowData', id).rowData);
      }else{
        delete selectData[id];
      }
    }else{
      selectData = {};
      selectData[id] = JSON.parse(dataGrid.dataGrid('getRowData', id).rowData);
    }
    initSelectTag();
  },
  onSelectAll: function(ids, isSelect){
    if ('${checkbox!}' == 'true'){
      for (var i=0; i<ids.length; i++){
        if(isSelect){
          selectData[ids[i]] = JSON.parse(dataGrid.dataGrid('getRowData', ids[i]).rowData);
        }else{
          delete selectData[ids[i]];
        }
      }
    }
    initSelectTag();
  },
  ondblClickRow: function(id, rownum, colnum, event){
    if ('${checkbox!}' != 'true'){
      js.layer.$('#' + window.name).closest('.layui-layer')
        .find(".layui-layer-btn0").trigger("click");
    }
    initSelectTag();
  }
});
function initSelectTag(){
  selectNum = 0;
  var html = [];
  $.each(selectData, function(key, value){
    selectNum ++;
    html.push('<span class="tag" id="'+key+'_tags-input"><span>'+value.userName+' </span>'
      + '<a href="#" onclick="removeSelectTag(\''+key+'\');" title="取消选择">x</a></span>');
  });
  html.unshift('<div class="title">当前已选择<span id="selectNum">'+selectNum+'</span>项：</div>');
  $('#selectData').empty().append(html.join(''));
}
function removeSelectTag(key){
  delete selectData[key];
  dataGrid.dataGrid('resetSelection', key);
  $('#selectNum').html(--selectNum);
  $('#'+key+'_tags-input').remove();
}
function getSelectData(){
  return selectData;
}
</script>
